package com.xs.langchain4j_springboot.mysql;

import com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Logger;

public class mysqlTest {
    public void testMethod() {
        // 数据库连接信息
        String jdbcUrl = "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC";
        String username = "your_username";
        String password = "your_password";

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. 注册驱动（JDBC 4.0 以后可省略）
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. 建立连接
            System.out.println("Connecting to database...");
            connection = DriverManager.getConnection(jdbcUrl, username, password);

            // 3. 创建Statement对象
            statement = connection.createStatement();

            // 4. 执行SQL查询
            String sql = "SELECT id, name, email FROM users"; // 替换为你自己的表名和字段
            resultSet = statement.executeQuery(sql);

            // 5. 处理结果集
            System.out.println("Query results:");
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");

                System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 6. 关闭资源（顺序很重要：后开先关）
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
